﻿package baidu.reader.common.model
{
    import baidu.reader.common.model.VO.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class LocalStorageModel extends Object
    {
        private static const IDENTIDIER:String = "app_new_reader";
        private static const MAX_SAVE_NUM:int = 20;
        private static var bookmarkArray:Array = [];
        public static var firstRun:Boolean = true;
        public static var currentPreference:AppPreferenceVO;
        public static var currentPosition:BookmarkItemVO;
        private static var saveTimer:Timer = new Timer(500, 1);

        public function LocalStorageModel()
        {
            return;
        }// end function

        public static function setCurrentPreference(param1:AppPreferenceVO) : void
        {
            SharedObject.getLocal(IDENTIDIER).data.preference = param1;
            SharedObject.getLocal(IDENTIDIER).flush();
            currentPreference = param1;
            return;
        }// end function

        public static function getCurrentPreference() : AppPreferenceVO
        {
            var preference:AppPreferenceVO;
            if (currentPreference)
            {
                return currentPreference;
            }
            try
            {
                preference = AppPreferenceVO.getVOfromObject(SharedObject.getLocal(IDENTIDIER).data.preference);
                LocalStorageModel.currentPreference = preference;
            }
            catch (e:Error)
            {
                trace(e.getStackTrace());
            }
            return currentPreference;
        }// end function

        public static function getCurrentSerialIDLastPosition(param1:String) : BookmarkItemVO
        {
            var item:BookmarkItemVO;
            var sid:* = param1;
            if (currentPosition)
            {
                return currentPosition;
            }
            try
            {
                bookmarkArray = SharedObject.getLocal(IDENTIDIER).data.bookmarks as Array;
                item = BookmarkItemVO.getVOfromObject(bookmarkArray[sid]);
            }
            catch (e:Error)
            {
                trace(e.getStackTrace());
            }
            if (!item)
            {
                firstRun = true;
            }
            else
            {
                firstRun = false;
            }
            return item;
        }// end function

        private static function doSaveBookmarkHandler(event:TimerEvent = null) : void
        {
            SharedObject.getLocal(IDENTIDIER).data.bookmarks = bookmarkArray;
            return;
        }// end function

        public static function setLastPosition(param1:BookmarkItemVO) : void
        {
            if (!bookmarkArray)
            {
                bookmarkArray = SharedObject.getLocal(IDENTIDIER).data.bookmarks as Array;
            }
            if (!bookmarkArray)
            {
                bookmarkArray = [];
            }
            bookmarkArray[param1.serialid] = param1;
            saveTimer.reset();
            saveTimer.start();
            currentPosition = param1;
            firstRun = false;
            return;
        }// end function

        saveTimer.addEventListener(TimerEvent.TIMER_COMPLETE, doSaveBookmarkHandler);
    }
}
